package resp

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

// TplResp 通用tpl响应
type TplResp struct {
	c   *gin.Context
	tpl string
}

// BuildTpl 返回一个tpl响应
func BuildTpl(c *gin.Context, tpl string) *TplResp {
	if !strings.HasSuffix(tpl, ".html") {
		tpl = tpl + ".html"
	}
	var t = TplResp{
		c:   c,
		tpl: tpl,
	}
	return &t
}

// ErrorTpl 返回一个错误的tpl响应
func ErrorTpl(c *gin.Context) *TplResp {
	var t = TplResp{
		c:   c,
		tpl: "error.html",
	}
	return &t
}

// ForbiddenTpl 返回一个无操作权限tpl响应
func ForbiddenTpl(c *gin.Context) *TplResp {
	var t = TplResp{
		c:   c,
		tpl: "error/unauth.html",
	}
	return &t
}

// WriteTpl 输出页面模板
func (resp *TplResp) WriteTpl(params ...gin.H) {
	if len(params) > 0 {
		resp.c.HTML(http.StatusOK, resp.tpl, params[0])
	} else {
		resp.c.HTML(http.StatusOK, resp.tpl, gin.H{})
	}
}
